<template>
  <div>
    <a-icon type="highlight" />
    系统设置:
    <div style="margin-bottom: 30px"></div>
    <div
      style="display: flex; flex-direction: row; justify-content: space-evenly"
    >
      <Card
        key="1"
        :content="data[0].type"
        style="margin: 15px"
        :init-value="data[0].data"
        @handleSubmit="success0"
      />
      <Card
        key="2"
        :content="data[1].type"
        style="margin: 15px"
        :init-value="data[1].data"
        @handleSubmit="success1"
      />
    </div>
    <div
      style="display: flex; flex-direction: row; justify-content: space-evenly"
    >
      <Card
        key="3"
        :content="data[2].type"
        style="margin: 15px"
        :init-value="data[2].data"
        @handleSubmit="success2"
      />
      <Card
        key="4"
        :content="data[3].type"
        style="margin: 15px"
        :init-value="data[3].data"
        @handleSubmit="success3"
      />
    </div>
    <div
        style="display: flex; flex-direction: row; justify-content: space-evenly">
      <Card
          key="3"
          :content="data[4].type"
          style="margin: 15px"
          :init-value="data[4].data"
          @handleSubmit="success4"
      />
    </div>
  </div>
</template>

<script>
import { getSystem } from "@/network/Info/BaseInfo";
import { changeData } from "@/network/System";
import Card from "./component/Card";
export default {
  components: { Card },
  data() {
    return { data: [] };
  },
  methods: {
    //专业方向
    async success0(value) {
      console.log(value);
      const name = value.data.names;
      const data = this.data[0].data;
      //判断是否有添加的
      const lines = [];
      data.forEach((item) => {
        lines.push(item.name);
      });
      //是否又增加
      for (const item of name) {
        const index = lines.indexOf(item);
        if (index === -1) {
          //新增的一个数
          console.log(item);
          const data = await changeData("pro", null, item, 1);
          if (data.msg) {
            await this.init();
          }
        }
      }
      //是否有删除
      for (const item of lines) {
        let index = lines.indexOf(item);
        if (name.indexOf(item) === -1) {
          const id = data[index].id;
          const res = await changeData("pro", id, null, 2);
          if (res.msg) await this.init();
        }
      }
    },
    //教研室
    async success1(value) {
      console.log(value);
      const name = value.data.names;
      const data = this.data[1].data;
      //判断是否有添加的
      const lines = [];
      data.forEach((item) => {
        lines.push(item.name);
      });
      //是否又增加
      for (const item of name) {
        const index = lines.indexOf(item);
        if (index === -1) {
          //新增的一个数
          console.log(item);
          const res = await changeData("subject", null, item, 1);
          if (res.msg) await this.init();
        }
      }
      //是否有删除
      for (const item of lines) {
        let index = lines.indexOf(item);
        if (name.indexOf(item) === -1) {
          const id = data[index].id;
          const res = await changeData("subject", id, null, 2);
          if (res.msg) await this.init();
        }
      }
    },
    //项目类型
    async success2(value) {
      console.log(value);
      const name = value.data.names;
      const data = this.data[2].data;
      //判断是否有添加的
      const lines = [];
      data.forEach((item) => {
        lines.push(item.name);
      });
      //是否又增加
      for (const item of name) {
        const index = lines.indexOf(item);
        if (index === -1) {
          //新增的一个数
          console.log(item);
          const res = await changeData("project", null, item, 1);
          if (res.msg) await this.init();
        }
      }
      //是否有删除
      for (const item of lines) {
        let index = lines.indexOf(item);
        if (name.indexOf(item) === -1) {
          const id = data[index].id;
          const res = await changeData("project", id, null, 2);
          if (res.msg) await this.init();
        }
      }
    },
    //竞赛类型
    async success3(value) {
      console.log(value);
      const name = value.data.names;
      const data = this.data[3].data;
      //判断是否有添加的
      const lines = [];
      data.forEach((item) => {
        lines.push(item.name);
      });
      //是否又增加
      for (const item of name) {
        const index = lines.indexOf(item);
        if (index === -1) {
          //新增的一个数
          console.log(item);
          const res = await changeData("match", null, item, 1);
          if (res.msg) await this.init();
        }
      }
      //是否有删除
      for (const item of lines) {
        let index = lines.indexOf(item);
        if (name.indexOf(item) === -1) {
          const id = data[index].id;
          const res = await changeData("match", id, null, 2);
          if (res.msg) await this.init();
        }
      }
    },
    async success4(value) {
      console.log(value);
      const name = value.data.names;
      const data = this.data[4].data;
      //判断是否有添加的
      const lines = [];
      data.forEach((item) => {
        lines.push(item.name);
      });
      //是否又增加
      for (const item of name) {
        const index = lines.indexOf(item);
        if (index === -1) {
          //新增的一个数
          console.log(item);
          const res = await changeData("teaProject", null, item, 1);
          if (res.msg) await this.init();
        }
      }
      //是否有删除
      for (const item of lines) {
        let index = lines.indexOf(item);
        if (name.indexOf(item) === -1) {
          const id = data[index].id;
          const res = await changeData("teaProject", id, null, 2);
          if (res.msg) await this.init();
        }
      }
    },
    async init() {
      const data = await getSystem();
      this.data = data.data;
      console.log(this.data[0].type);
      this.data.forEach((item, index) => {
        item.success = "success" + index;
      });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style lang="less" scoped>
</style>